package operation;

import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        System.out.println("请输入要删除的书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int current = bookList.getUsedSize();
        int pos = -1;
        for (int i = 0; i < current; i++) {
            if (name.equals(bookList.getBook(i).getName()) == true) {
                pos = i;
                break;
            }
        }
        if (pos == -1) {
            System.out.println("找不到要删除的书");
        } else if (pos == current) {
            bookList.setUsedSize(current - 1);
            bookList.setBooks(current,null);
        } else {
            for (int i = 0; i < current - 1; i++) {
                bookList.setBooks(i, bookList.getBook(i + 1));
            }
            bookList.setBooks(current, null);
        }
    }
}
